Core.Storage.Notifier = function() {
	this.dirty = [];
	this.visited = {};
}

Core.Storage.Notifier.prototype = {
	
	__clear: function() {
		this.dirty = [];
		this.visited = {};
	},
	
	/**
	 * Sets the given listener as dirty.
	 * @param Function fn
	 */
	setDirty: function(fn) {
		if(!this.visited[Env.stamp(fn)]) {
			this.visited[Env.stamp(fn)] = true;
			this.dirty.push(fn);
		}
	},
	
	/**
	 * Invokes all listeners from dirty NPs.
	 * Executes until dirty array is empty.
	 * 
	 * Will silently ignore if invoked listener would set itself as dirty.
	 * Will not protect against cyclical-dirty-invocations. 
	 */
	notify: function() {
		while(!this.dirty.empty()) {
			var	fn = this.dirty.shift();
			
			fn();
			this.visited[Env.stamp(fn)] = false;
		}
	}
};

Core.Storage.Notifier = Singleton.register("StorageNotifier", Core.Storage.Notifier);

Core.Scheduler.periodic(function(){ Core.Storage.Notifier.notify(); }, 10);